﻿using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Application.Bussiness;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Data;
using System.Collections;

namespace LeSoft.Application.Web.Controllers
{

    [HandlerLogin(LoginMode.Enforce)]
    public class HomeController : Controller
    {
        private TaskBLL taskBLL = new TaskBLL();
        #region 视图
        public ActionResult Index()
        {

            return View();
        }
        public ActionResult Main()
        {
            return View();
        }
        public ActionResult Desktop()
        {
            return View();
        }
        public ActionResult SearchForm()
        {
            return View();
        }
        #endregion
        /// <summary>
        /// 访问功能
        /// </summary>
        /// <param name="moduleId">功能Id</param>
        /// <param name="moduleName">功能模块</param>
        /// <param name="moduleUrl">访问路径</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult VisitModule(string moduleId, string moduleName, string moduleUrl)
        {
            LogEntity logEntity = new LogEntity();
            logEntity.CategoryId = 2;
            logEntity.OperateTypeId = ((int)OperationType.Visit).ToString();
            logEntity.OperateType = EnumAttribute.GetDescription(OperationType.Visit);
            logEntity.OperateAccount = OperatorProvider.Provider.Current().Account;
            logEntity.OperateUserId = OperatorProvider.Provider.Current().UserId;
            logEntity.ModuleId = moduleId;
            logEntity.Module = moduleName;
            logEntity.ExecuteResult = 1;
            logEntity.ExecuteResultJson = "访问地址：" + moduleUrl;
            logEntity.WriteLog();
            return Content(moduleId);
        }

        public ActionResult GetCharacter()
        {
            BussinessIndicatorBLL bussinessIndicatorBLL = new BussinessIndicatorBLL();
            BussinessIndicatorRuleBLL bussinessIndicatorRuleBLL = new BussinessIndicatorRuleBLL();
            var list = bussinessIndicatorBLL.GetList();
            if (!OperatorProvider.Provider.Current().IsSystem)
            {
                list = list.Where(t => OperatorProvider.Provider.Current().CompanyId.Equals(t.OrganizeId));
            }
            var dataList = list.Join(bussinessIndicatorRuleBLL.GetList().Where(t => t.AllowShow == 1), x => x.RuleId, y => y.Id, (x, y) => new
            {
                x.Id,
                x.CountUserName,
                x.Date,
                x.Month,
                x.Time,
                x.Value,
                x.Year,
                x.RuleName,
                y.ValueFlag,
                y.Icon,
                Color = GetColor(x, y)
            }).ToList();
            var _Html = "";
            foreach (var item in dataList)
            {
                _Html += string.Format(@"<div class='col-xs-6'>
                                         <a href='javascript:;' onclick='OpenNav(&apos;{3}&apos;,3)'>
                                         <div class='panel padder-v item ' style='background-color: {0};'>
                                         <div class='h1 text-fff font-thin h1'>{1}</div>
                                         <h3>{2}</h3>
                                        </a>
                                        </div>
                                        </div>
                                           ", item.Color, item.Value, item.RuleName, item.Id);
            }
            return Content(new { html = _Html, count = dataList.Count() }.ToJson());
        }

        public string GetColor(BussinessIndicatorEntity x, BussinessIndicatorRuleEntity y)
        {
            var obj = new[] { new { Id = "", Flag = "", ValueMemo = "", MinValue = "1", MaxValue = "", Color = "" } };
            obj = JsonConvert.DeserializeAnonymousType(y.ValueFlag, obj);
            foreach (var item in obj)
            {
                switch (item.Flag)
                {
                    case "0": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) < Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) > Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "1": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "2": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) == Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) == Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "3": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "4": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) > Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) < Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "5": if ((Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MinValue) && Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MaxValue)) || (Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MinValue) && Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MaxValue))) { return item.Color; } break;
                    default:
                        break;
                }
            }
            return "#CECECE";
        }

        
        [HttpGet]
        [HandlerAction(OperationType.GetList, "首页", "获取公告通知列表")]
        public ActionResult GetNotice()
        {
            string retStr = string.Empty;
            int datacount = 0;
            var data = new NoticeBLL().GetList().ToList();
            if (data.Count > 0)
            {

                datacount = data.Count;
                foreach (NewsEntity entity in data)
                {
                    retStr += "<div class='vertical-timeline-block'>";
                    retStr += " <div class='vertical-timeline-icon navy-bg'><i class='fa fa-briefcase'></i></div>";
                    retStr += " <div class='vertical-timeline-content'> <h2>" + entity.Category + "</h2>" + "<p>" + entity.FullHead + "</p>";
                    retStr += string.Format("<a  class='btn btn-sm btn-primary' onclick=\"btn_NoticeDetail('{0}')\"> 更多信息</a> <span class='vertical-date'><small>" + Time.GetChineseDateTime(entity.ReleaseTime.ToDate()) + "</small></span></div>", entity.NewsId);
                    retStr += " </div>";
                }
            }
            var JsonData = new
            {
                html = retStr,
                count = datacount
            };
            return Content(JsonData.ToJson());
        }
        [HttpGet]
        [HandlerAction(OperationType.GetList, "首页", "获取工单统计")]
        public ActionResult GetCountBill()
        {
            string[] Name = new string[]{"联系", "维修", "投诉", "开闭店", "装修" };
            ArrayList list = new ArrayList();
            
            DataTable dt = new LinkBillBLL().GetAllCount();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(dt.Rows[i][0].ToString());
                }
            }
            string[] Value = (string[])list.ToArray(typeof(string));
            var JsonData = new
            {
                datax = Name,
                datay = Value
            };
            return Content(JsonData.ToJson()); 

        }

    }
}
